package com.chinapost.action;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.springframework.beans.factory.annotation.Autowired;

import com.chinapost.po.Tmonth;
import com.chinapost.util.ImportFileUtil;
import com.chinapost.vo.Json;

@Action(value = "importAction")
public class ImportAction extends BaseAction  {

	private String importPath = ServletActionContext.getServletContext().getRealPath(  
            "/") + "WEB-INF\\data";
	private String filename;
	
	private Integer cmonth;
	
	@Autowired
	private MonthServiceI monthService;
	

	public Integer getCmonth() {
		return cmonth;
	}

	public void setCmonth(Integer cmonth) {
		this.cmonth = cmonth;
	}


	public String getFilename() {
		return filename;
	}

	public void setFilename(String filename) {
		this.filename = filename;
	}


	public void importDqcd(){
		Json j = new Json();
		ImportFileUtil ifu = ImportFileUtil.getInstance(importPath);
		String ctlfile=importPath+File.separator+"dqcd.ctl";
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
		String badfile=importPath+File.separator+"log"+File.separator+df.format(new Date())+"_tdqcd.bad";
		boolean b = ifu.importData("TDQCD",filename, ctlfile,badfile);
		if (b){
			//保存当前定期存单数据的上传月份
			Tmonth t = new Tmonth(1,cmonth);
			monthService.add(t);
		j.setSuccess(true);
		j.setMsg("导入数据成功！");
		}else{
			j.setSuccess(false);
			j.setMsg("导入数据失败！");
		}
		super.writeJson(j);
	}
	
	public void importDqlkt(){
		Json j = new Json();
		ImportFileUtil ifu = ImportFileUtil.getInstance(importPath);
		String ctlfile=importPath+File.separator+"dqlkt.ctl";
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
		String badfile=importPath+File.separator+"log"+File.separator+df.format(new Date())+"_tdqlkt.bad";
		boolean b = ifu.importData("TDQLKT",filename, ctlfile,badfile);
		if (b){
			//保存当前绿卡通定期存单数据的上传月份
			Tmonth t = new Tmonth(2,cmonth);
			monthService.add(t);
		j.setSuccess(true);
		j.setMsg("导入数据成功！");
		}else{
			j.setSuccess(false);
			j.setMsg("导入数据失败！");
		}
		super.writeJson(j);
	}
	
	
	public void importDqobk(){
		Json j = new Json();
		ImportFileUtil ifu = ImportFileUtil.getInstance(importPath);
		String ctlfile=importPath+File.separator+"dqobk.ctl";
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
		String badfile=importPath+File.separator+"log"+File.separator+df.format(new Date())+"_tdqobk.bad";
		boolean b = ifu.importData("tdqobk",filename, ctlfile,badfile);
		if (b){
			//保存当前一本通定期存单数据的上传月份
			Tmonth t = new Tmonth(3,cmonth);
			monthService.add(t);
		j.setSuccess(true);
		j.setMsg("导入数据成功！");
		}else{
			j.setSuccess(false);
			j.setMsg("导入数据失败！");
		}
		super.writeJson(j);
	}
}